﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;

namespace _4T.FileStorage
{
    public partial class ReceiveFile : System.Web.UI.Page
    {
        private List<string> imageArrayPath = new List<string>();
        private List<string> videoArrayPath = new List<string>();
        Dictionary<string, int> sizesToMake = new Dictionary<string, int>();
        private int sizeMedium = 500;
        int NewWidth = 0;
        int NewHeight = 0;
        string saveToPhotoFolder = "Store/Photos";
        string saveToVideoFolder = "Store/Videos";
        protected void Page_Load(object sender, EventArgs e)
        {
            sizesToMake.Add("M", sizeMedium);
            if (!IsPostBack)
            {
                List<string> arrayImagePostBack = Session["imageArrayPath"] as List<string>;
                if (arrayImagePostBack != null && arrayImagePostBack.Count > 0)
                    imageArrayPath = arrayImagePostBack;

                List<string> arrayVideoPostBack = Session["videoArrayPath"] as List<string>;
                if (arrayVideoPostBack != null && arrayVideoPostBack.Count > 0)
                    videoArrayPath = arrayVideoPostBack;
            }

            if (!Directory.Exists(Server.MapPath(saveToPhotoFolder)))
                Directory.CreateDirectory(Server.MapPath(saveToPhotoFolder));
            if (!Directory.Exists(Server.MapPath(saveToVideoFolder)))
                Directory.CreateDirectory(Server.MapPath(saveToVideoFolder));

            HttpFileCollection uploadedFiles = Request.Files;
            for (int i = 0; i < uploadedFiles.Count; i++)
            {
                HttpPostedFile F = uploadedFiles[i];
                if (uploadedFiles[i] != null && F.ContentLength > 0)
                {
                    string uploadedFileName = F.FileName.Substring(F.FileName.LastIndexOf("\\") + 1);
                    string extension = uploadedFileName.Substring(uploadedFileName.LastIndexOf(".") + 1);
                    Guid nameRandom = Guid.NewGuid();
                    string Path = Server.MapPath(saveToVideoFolder);
                    string filePath = Path + "\\" + nameRandom + "." + extension;
                    string videoPath = "FileStorage/" + saveToVideoFolder + "/" + nameRandom;

                    if (extension.ToLower() == "mp3".ToLower() || extension.ToLower() == "mp4".ToLower() || extension.ToLower() == "wmv".ToLower() || extension.ToLower() == "flv".ToLower() || extension.ToLower() == "wma".ToLower())
                    {
                        F.SaveAs(filePath);
                        videoPath += "." + extension.ToLower();
                        videoArrayPath.Clear();
                        videoArrayPath.Add(videoPath);
                        Session["videoArrayPath"] = null;
                        Session["videoArrayPath"] = videoArrayPath;
                        break;
                    }
                }
            }

            for (int i = 0; i < uploadedFiles.Count; i++)
            {
                HttpPostedFile F = uploadedFiles[i];
                if (uploadedFiles[i] != null && F.ContentLength > 0)
                {
                    string uploadedFileName = F.FileName.Substring(F.FileName.LastIndexOf("\\") + 1);
                    string extension = uploadedFileName.Substring(uploadedFileName.LastIndexOf(".") + 1);
                    Guid nameRandom = Guid.NewGuid();
                    string Path = Server.MapPath(saveToPhotoFolder);
                    string filePath = Path + "\\" + nameRandom + "." + extension;
                    string photoPath = "FileStorage/" + saveToPhotoFolder + "/" + nameRandom;

                    if (extension.ToLower() == "jpg".ToLower() || extension.ToLower() == "gif".ToLower() || extension.ToLower() == "jpeg".ToLower() || extension.ToLower() == "png".ToLower())
                    {
                        F.SaveAs(filePath);
                        photoPath += "__M." + extension.ToLower();
                        Resize(F, saveToPhotoFolder, nameRandom, extension);
                        imageArrayPath.Clear();
                        imageArrayPath.Add(photoPath);
                        Session["imageArrayPath"] = null;
                        Session["imageArrayPath"] = imageArrayPath;
                        break;
                    }
                }
            }
        }

        public void Resize(HttpPostedFile F, string SaveToFolder, Guid SystemFileNamePrefix, string Extension)
        {
            //Makes all the different sizes in the sizesToMake collection
            foreach (KeyValuePair<string, int> pair in sizesToMake)
            {
                using (System.Drawing.Image image = System.Drawing.Image.FromStream(F.InputStream))
                //determine the thumbnail sizes
                using (Bitmap bitmap = new Bitmap(image))
                {
                    decimal Ratio;
                    if (bitmap.Width > bitmap.Height)
                    {
                        Ratio = (decimal)pair.Value / bitmap.Width;
                        NewWidth = pair.Value;
                        decimal Temp = bitmap.Height * Ratio;
                        NewHeight = (int)Temp;
                    }
                    else
                    {
                        Ratio = (decimal)pair.Value / bitmap.Height;
                        NewHeight = pair.Value;
                        decimal Temp = bitmap.Width * Ratio;
                        NewWidth = (int)Temp;
                    }
                }

                using (System.Drawing.Image image = System.Drawing.Image.FromStream(F.InputStream))
                using (Bitmap bitmap = new Bitmap(image, NewWidth, NewHeight))
                {
                    bitmap.Save(Server.MapPath(SaveToFolder + "/" + SystemFileNamePrefix.ToString() + "__" + pair.Key + "." + Extension), image.RawFormat);
                }
            }
        }
    }
}